package com.lvtongka.app.ui.setting

import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.google.gson.Gson
import com.lvtongka.app.base.BaseActivity
import com.lvtongka.app.common.CommonUtils
import com.lvtongka.app.databinding.ActivitySettingBinding
import com.lvtongka.app.dialog.DialogSureCancel
import com.lvtongka.app.fragment.common.DialogHelper
import com.lvtongka.app.fragment.my.bean.MyResponse
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.http.common.WebConstants
import com.lvtongka.app.ui.security.SecurityActivity
import com.lvtongka.app.ui.webview.WebViewActivity

class SettingActivity : BaseActivity<ActivitySettingBinding, SettingViewModel>() {
    override fun initViewModel(): SettingViewModel {
        return ViewModelProvider(this)[SettingViewModel::class.java]
    }

    override fun initEvent() {
        binding.header.imgBack.setOnClickListener { finish() }
        viewModel.error.observe(this) {
            hideLoading()
            showToast(it)
        }
        viewModel.agreementList.observe(this) {
            hideLoading()
        }

        binding.llBindPhone.setOnClickListener {

        }

        binding.llSecurity.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            startActivityForResult(Intent(this, SecurityActivity::class.java), 1)
        }

        binding.llPayManager.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            jumpWeb(WebConstants.PAY_SETTING)
        }

        binding.llQuestions.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            jumpWeb(WebConstants.HELP_CENTER)
        }

        binding.llFamilySharing.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            jumpWeb(WebConstants.FAMILY_SHARE)
        }

        binding.llRiskAssessment.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            jumpWeb(WebConstants.RISK_LEVEL)
        }

        binding.tvLogout.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            showLogOffDialog()
        }
    }

    override fun initData() {
        binding.header.tvTitle.text = "设置"
        binding.header.imgBack.visibility = View.VISIBLE
        binding.header.statusBarView.layoutParams.height = CommonUtils.getStatusBarHeight()

        var json = SPHelper.getString(SPKConstants.BASIC_INFO)
        if (!TextUtils.isEmpty(json)) {
            var info = Gson().fromJson(json, MyResponse::class.java)
            binding.tvBindPhone.text = info.data.cell
        }
    }

    override fun initBinding(): ActivitySettingBinding {
        return ActivitySettingBinding.inflate(layoutInflater)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1 && resultCode == RESULT_OK) {
            setResult(RESULT_OK)
            finish()
        }
    }

    private fun showLogOffDialog() {
        val sureCancel = DialogSureCancel(this)
        sureCancel.setTitle("提示")
        sureCancel.setContent("确定要退出登录吗？")
        sureCancel.setCancelable(false)
        sureCancel.setCanceledOnTouchOutside(false)
        sureCancel.setSureListener {
            sureCancel.cancel()
            SPHelper.clearLoginInfo()
            showToast("退出登录成功")
            setResult(RESULT_OK)
            finish()
        }
        sureCancel.setCancelListener {
            sureCancel.cancel()
        }
        sureCancel.show()
    }
}